class Node{
    Node next;
    Node prve;
    int val;
    public Node(int val){
        this.val = val;
    }
}
public class MyLinkedList {
        Node head;
        int size;
        public MyLinkedList() {
            this.size = 0;
            this.head = null;
        }

        public void DisplayList(){
            if(head==null) return;
            Node cur = head;
            while(cur!=null){
                System.out.print(cur.val+" ");
                cur = cur.next;
            }
            System.out.println();
        }

        public int get(int index) {
            if(index<0||index>=size) return -1;
            Node cur = head;
            while(index!=0){
                cur = cur.next;
                index--;
            }
            return cur.val;
        }

        public void addAtHead(int val) {
            Node node = new Node(val);
            node.next = head;
            head = node;
            size++;
        }

        public void addAtTail(int val) {
            Node node = new Node(val);
            if(head==null){
                head = node;
                size++;
            }else{
                Node cur = head;
                while(cur.next!=null){
                    cur = cur.next;
                }
                cur.next = node;
                size++;
            }
        }

        public void addAtIndex(int index, int val) {
            if(index<0||index>size){
                return;
            }
            if(index==0){
                addAtHead(val);
                return;
            }
            if(index==size){
                addAtTail(val);
                return;
            }
            Node node = new Node(val);
            Node cur = head;
            while(index!=0){
                cur = cur.next;
                index--;
            }
            cur.prve.next = node;
            node.prve = cur.prve;
            node.next = cur;
            cur.prve = node;
            size++;
        }

        public void deleteAtIndex(int index) {
            if(head==null){
                return;
            }
            if(index<0||index>=size){
                return;
            }
            Node cur = head;
            while(index!=0){
                cur = cur.next;
                index--;
            }
            if(cur == head){
                head = head.next;
                head.prve = null;
                size--;
            }else{
                cur.prve.next = cur.next;
                if(cur.next!=null){
                    cur.next.prve = cur.prve;
                    size--;
                }else{
                    cur.prve.next = null;
                    size--;
                }
            }
        }
}
